<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

const router = useRouter()
const store = useStore()
const getters = store.getters

// 粒子特效页面的个数，用于随机显示特效页
let loginIframe = ref(1)
const loginParticleNum = 18

// 显示站点名称
const systemName = computed(() => {
    let title = getters.systemConfig && getters.systemConfig.systemName || import.meta.env.VITE_APP_SiteName
    document.title = title
    return title
})

// 随机显示粒子特效
const showIframe = (() => {
    const particleNum = Math.floor(Math.random() * loginParticleNum + 1)
    const canotShows = [2, 4, 17]
    let index = canotShows.indexOf(particleNum)
    loginIframe = index === -1 ? particleNum : loginParticleNum
})()

// 登录
let isLoading = ref(false)
const loginData = ref({
    client_id: "default",
    grant_type: "password",
    username: "",
    password: "",
    scope: "flcx:analysis"
})
const login = () => {
    let submitData = loginData.value
    if (!submitData.username) {
        window.$Common.warningMsg("请输入用户名!")
        return false
    }
    if (!submitData.password) {
        window.$Common.warningMsg("请输入密码!")
        return false
    }
    isLoading.value = true
    let commitData = new URLSearchParams()
    commitData.append("client_id", submitData.client_id)
    commitData.append("grant_type", submitData.grant_type)
    commitData.append("username", submitData.username)
    commitData.append("password", submitData.password)
    commitData.append("scope", submitData.scope)
    // 判断是否需要请求api
    let isRequestApi = import.meta.env.VITE_APP_IsRequestApi
    if (!isRequestApi || isRequestApi === "true") {
        // 正常的请求api登录
        store.dispatch("login", commitData).then(res => {
            isLoading.value = false
            router.push("/")
        }).catch(e => {
            isLoading.value = false
        })
    } else {
        // 本地随便一个帐号密码登录
        store.dispatch("loginLocal", submitData.username).then(res => {
            isLoading.value = false
            router.push("/")
        }).catch(e => {
            isLoading.value = false
        })
    }
}

// 回车事件
const enterKey = (event) => {
    const code = event.keyCode
        ? event.keyCode
        : event.which
            ? event.which
            : event.charCode
    if (code == 13) {
        login()
    }
}
</script>

<template>
    <div class="login_box">
        <iframe id="loginParticle" :src="'static/particle/' + loginIframe + '.html'" class="login_iframe" />
        <div class="login_empty"></div>
        <div class="login_main">
            <el-form ref="loginForm" :model="loginData">
                <el-form-item>
                    <div class="login_title tc">{{ systemName }}</div>
                </el-form-item>
                <el-form-item>
                    <el-input class="elliptic" v-model="loginData.username" placeholder="用户名" size="large" @keyup.enter="login">
                        <template #prefix><i class="el-input__icon fa fa-user"></i></template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-input class="elliptic" type="password" v-model="loginData.password" placeholder="密码"  size="large" @keyup.enter="login">
                        <template #prefix><i class="el-input__icon fa fa-lock"></i></template>
                    </el-input>
                </el-form-item>
                <el-form-item label="记住密码：" class="keep_password" v-if="false">
                    <el-switch active-color="#13ce66" inactive-color="#ddd"  size="large"></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button class="elliptic" type="primary"  size="large" @click="login" :loading="isLoading">登 录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.login_box {
    width: 100%;
    height: 100vh;
    background: #00ffba;
    .login_empty {
      padding-top: 20vh;
    }
    .login_iframe {
      width: 100%;
      height: 100%;
      border: 0;
      position: absolute;
      opacity: .8;
      left: 0;
      top: 0;
    }
    .login_main {
      position: absolute;
      width: 300px;
      left: 50%;
      margin-left: -180px;
      padding: 20px 30px;
      background: #fff;
      border-radius: 12px;
      .keep_password {
        .el-form-item__content { 
            text-align: left;
        }
      }
      .el-button {
        width: 100%;
      }
      .login_title {
        width: 100%;
        font-size: 20px;
        font-weight: bold;
      }
      .el-input__icon {
        color: var(--el-color-primary);
      }
    }
    :deep() {
        .elliptic {
            border-radius: 30px;
            .el-input__wrapper {
                border-radius: 30px;
            }
        }
    }
  }
  @media screen and (max-width: 320px) {
    .login_box {
      .login_main {
        width: 250px;
        margin-left: -155px;
      }
    }
  }
</style>